package com.nativetalk.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by lizhun on 15/12/23.
 */
public class CommonUtil {
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    private static SimpleDateFormat sdf_channel = new SimpleDateFormat("MddHH");
    private static Date date = new Date();

    /**
     * 根据时间判断周几
     * @param date
     * @return
     */
    public static Integer getWeekday(String date){//必须yyyy-MM-dd
        int result = 1;
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdw = new SimpleDateFormat("E");
        Date d = null;
        try {
            d = sd.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String week = sdw.format(d);
        switch (week) {
            case "星期一":
                result = 1;
                break;
            case "星期二":
                result = 2;
                break;
            case "星期三":
                result = 3;
                break;
            case "星期四":
                result = 4;
                break;
            case "星期五":
                result = 5;
                break;
            case "星期六":
                result = 6;
                break;
            case "星期日":
                result = 7;
                break;

        }
        return result;
    }
    /**
     * @Purpose  获取当前月份
     * @author   lizhun
     * @version  1.0
     * @return   String
     */
    public static   String getMonth(){
        return simpleDateFormat.format(date);
    }
    /**
     * @Purpose  生成token
     * @author   lizhun
     * @version  1.0
     * @return   String
     */
    public static String getToken(){
        UUID token = UUID.randomUUID();
        return ""+token;
    }
    /**
     * @Purpose  生成邀请码
     * @author   lizhun
     * @version  1.0
     * @return   String
     */
    public static String getInviteCode(){
        String code = "" + (int) ((Math.random() * 9 + 1) * 100);
        String dateString = code + sdf.format(date);
        Long result = Long.valueOf(dateString);
        String inviteCode = result.toHexString(result);
        return  inviteCode.substring(0,inviteCode.length()-4);
    }
    /**
     * @Purpose  生成直播频道
     * @author   lizhun
     * @version  1.0
     * @return   String
     */
    public static String getLiveChannel(){
        String code = "" + (int) ((Math.random() * 9 + 1) * 1000);
        String dateString = code + sdf_channel.format(date);
        return  dateString;
    }
    /**
     * @Purpose  计算年龄
     * @author   lizhun
     * @version  1.0
     * @return   String
     */
    public static int getAge(Date dateOfBirth) {
        int age = 0;
        Calendar born = Calendar.getInstance();
        Calendar now = Calendar.getInstance();
        if (dateOfBirth != null) {
            now.setTime(new Date());
            born.setTime(dateOfBirth);
            if (born.after(now)) {
                throw new IllegalArgumentException(
                        "Can't be born in the future");
            }
            age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
            if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) {
                age -= 1;
            }
        }
        return age;
    }
    /**
     * @Purpose  获取时间
     * @author   lizhun
     * @version  1.0
     * @return   String
     */
    public static Long getTime(int set_type,int size) {
        int dateSize = 0;
        switch (set_type) {
            case 1:
                dateSize = 1;
                break;
            case 2:
                dateSize = 7;
                break;
            case 3:
                dateSize = 31;
                break;
            case 4:
                dateSize = 365;
                break;

            default:
                break;
        }
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, dateSize*size);
//        Date date = new Date();
//        date.setDate(dateSize*size);
        return c.getTimeInMillis();
    }
    /**
     * @Purpose  获取编号
     * @author   lizhun
     * @version  1.0
     * @return   String
     */
    public static String getOrderNub() {
        String orderNub = sdf.format(date).substring(2, 12);

        String code = "";
        while(code.length()<5)
        {
            code+=(int)(Math.random()*10);
        }
        orderNub = orderNub + code;
        return orderNub;
    }
    /**
     * 判断时间是否在时间段内
     *

     * @return
     */
    public static boolean isInDate(Date time, String startTime,
                                   String endTime) {
        Boolean b=false;
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String nowTime = time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
        try {
            time = sdf.parse(nowTime);
            Date startTimes = sdf.parse(startTime);
            Date endTimes = sdf.parse(endTime);
            if(startTimes.after(endTimes)){
                Date n = startTimes;
                startTimes = endTimes;
                endTimes = n;
            }
            if (time.after(startTimes) || time.equals(startTimes)){
                if (time.before(endTimes) || time.equals(endTimes)){
                    b=true;
                }else{
                    b=false;
                }
            }else{
                b=false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return b;
    }
    public static int getMinute(Date firstDate, Date secondDate, String time){
        Calendar c = Calendar.getInstance();
        int second=0;
        c.setTime(secondDate);
        int year = c.get(Calendar.YEAR);
        int mouth = c.get(Calendar.MONTH)+1;
        int day = c.get(Calendar.DATE);

        String endtime = year+"-"+mouth+"-"+day+" "+time;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            secondDate = sdf.parse(endtime);
            Long l = (firstDate.getTime()-secondDate.getTime());
            second = (int) (l/1000/60);
            if (l%60 !=0){
                second = second+1;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return second;
    }

    /**
     * 判断字符串是否为数字
     */
    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }
}
